﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Data;
using System.Data.Linq;
using System.Drawing;
using System.Text;
using System.Management;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Management;
namespace UI.Forms
{
    public partial class FrmPrintSet : DevExpress.XtraEditors.XtraForm
    {
        public FrmPrintSet()
        {
            InitializeComponent();
        }

        private void imageListBoxControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (imageListBoxControl1.SelectedIndex != -1)
            {
                foreach (DevExpress.XtraEditors.Controls.ImageListBoxItem item in imageListBoxControl1.Items)
                {
                    if (imageListBoxControl1.Items[imageListBoxControl1.SelectedIndex].Value==item.Value)
                    {
                        item.ImageIndex = 1;
                    }
                    else
                    {
                        item.ImageIndex = 0;
                    }
                }

            }
            else
            {
                foreach (DevExpress.XtraEditors.Controls.ImageListBoxItem item in imageListBoxControl1.Items)
                {

                        item.ImageIndex = 0;

                }
            }

        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
             DevExpress.XtraEditors.Controls.ImageListBoxItem sli = imageListBoxControl1.SelectedItem as DevExpress.XtraEditors.Controls.ImageListBoxItem;
             MyConfig.setAppValue("curPrint",sli.Value.ToString());
             my.myprint=sli.Value.ToString();
             this.Close();
        }

        private void FrmPrintSet_Load(object sender, EventArgs e)
        {
            string wmiSQL = "SELECT * FROM Win32_Printer";
            ManagementObjectCollection printers = new ManagementObjectSearcher(wmiSQL).Get();
            bool youprint = false;
            foreach (ManagementObject printer in printers)
            {
                PropertyDataCollection.PropertyDataEnumerator pde = printer.Properties.GetEnumerator();


                if (printer.Properties["Name"].Value.ToString() == my.myprint)
                {
                    DevExpress.XtraEditors.Controls.ImageListBoxItem a = new DevExpress.XtraEditors.Controls.ImageListBoxItem(printer.Properties["Name"].Value.ToString(), 1);
                    imageListBoxControl1.Items.Add(a);
                    imageListBoxControl1.SelectedItem = a;
                    youprint = true;
                }
                else
                {
                    imageListBoxControl1.Items.Add(printer.Properties["Name"].Value.ToString(), 0);
                }


            }
            if (!youprint)
            {
                imageListBoxControl1.SelectedIndex=-1;
            }

        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}